const product = require('../../templates/product/list');

Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    height: 64, //header高度
    top: 0, //标题图标距离顶部距离
    scrollH: 0, //滚动总高度
    opcity: 0,
    iconOpcity: 0.5,
    member: null,
    productList: [],
    pageIndex: 1,
    loading: false,
    noMore: false
  },
  onLoad: function(options) {
    let obj = wx.getMenuButtonBoundingClientRect(),
        config = wx.getStorageSync('rf_config');
    this.setData({
      copyright: config.copyright_desc,
      navigate: [
        {url: '../help/aboutUs',type: 'navigate',text: config.copyright_companyname}
      ]
    });
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          width: obj.left || res.windowWidth,
          height: obj.top ? (obj.top + obj.height + 8) : (res.statusBarHeight + 44),
          top: obj.top ? (obj.top + (obj.height - 32) / 2) : (res.statusBarHeight + 6),
          scrollH: res.windowWidth * 0.6
        });
      }
    });
  },
  onShow: async function () {
    let isLogined = await wx.user.hasLogin();
    this.setData({
      isLogined: isLogined
    });
    if (isLogined) {
      // 更新用户信息
      this.setData({
        member: await wx.user.memberInfo()
      });
      /*this.setData({
        member: wx.getStorageSync('rf_member')
      });*/
      // 未读消息数
      wx.util.request('message.unreadNum').then(res => {
        this.setData({
          unreadNum: res.count
        });
      });
      // 猜你喜欢
      wx.util.request('product.guess').then(res => {
        this.setData({
          productList: res
        });
      });
    } else if (this.data.member) {
      this.setData({
        member: null
      });
    }
  },
  onPageScroll(e) {
    let scroll = e.scrollTop <= 0 ? 0 : e.scrollTop,
        opcity = scroll / this.data.scrollH;
    if (this.data.opcity >= 1 && opcity >= 1) return;
    this.setData({
      scroll: scroll,
      opcity: opcity,
      iconOpcity: 0.5 * (1 - opcity < 0 ? 0 : 1 - opcity)
    });
  },
  onPullDownRefresh() {
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 200);
  },
  onReachBottom: function() {
    if (!this.data.pullUpOn) return;
    this.setData({
      loading:true
    });
    if (this.data.pageIndex == 4) {
      this.setData({
        loading: false,
        noMore:false
      });
    } else {
      let loadData = JSON.parse(JSON.stringify(this.data.productList));
      loadData = loadData.splice(0, 10);
      if (this.data.pageIndex == 1) {
        loadData = loadData.reverse();
      }
      this.setData({
        loading: false,
        pageIndex: this.data.pageIndex + 1,
        productList: this.data.productList.concat(loadData)
      });
    }
  },
  userLogin: function (e) {
    if (!e.detail.userInfo) {
      wx.util.toast('您已取消登录');
      return;
    }
    wx.user.login(this);
  },
  href(e) {
    if (!this.data.isLogined) {
      wx.util.toast('请登录');
      return;
    }
    let target = e.currentTarget.dataset,
        url = `../${target.dir}/${target.page || 'index'}?`;
    switch (target.page) {
      case 'set':
        url += `nickname=${this.data.member.nickname}&avatar=${this.data.member.head_portrait}`;
        break;
      case 'my':
        url += `tab=${target.tab}`;
        break;
      default:
        break;
    }
    if (target.dir) {
      wx.navigateTo({
        url: url
      });
    } else {
      wx.util.toast('敬请期待~');
    }
  },
  detail: function (e) {
    product.detail(e);
  },
  similar: function (e) {
    product.similar(e.currentTarget.dataset.kw);
  }
})